import React, { useState } from 'react';
import { Form, Input, Button, Card, message } from 'antd';
import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons';
import { useNavigate } from 'react-router-dom';
import { useUser } from '../contexts/UserContext';
import { IUser } from '../types';

const Register: React.FC = () => {
    const [loading, setLoading] = useState(false);
    const navigate = useNavigate();
    const { setUser } = useUser();
    const [form] = Form.useForm();

    const onFinish = async (values: {
        username: string;
        email: string;
        password: string;
        confirmPassword: string;
    }) => {
        if (values.password !== values.confirmPassword) {
            message.error('两次输入的密码不一致！');
            return;
        }

        setLoading(true);
        try {
            // TODO: 调用注册API
            const newUser: IUser = {
                id: Math.random().toString(36).substr(2, 9),
                username: values.username,
                email: values.email,
                createdAt: new Date().toISOString()
            };
            localStorage.setItem('user', JSON.stringify(newUser));
            setUser(newUser);
            message.success('注册成功！');
            navigate('/');
        } catch (error) {
            message.error('注册失败，请重试！');
        } finally {
            setLoading(false);
        }
    };

    return (
        <div style={{ maxWidth: 400, margin: '0 auto', marginTop: 50 }}>
            <Card title="用户注册" bordered={false}>
                <Form
                    name="register"
                    form={form}
                    onFinish={onFinish}
                >
                    <Form.Item
                        name="username"
                        rules={[
                            { required: true, message: '请输入用户名！' },
                            { min: 3, message: '用户名至少3个字符！' }
                        ]}
                    >
                        <Input
                            prefix={<UserOutlined />}
                            placeholder="用户名"
                        />
                    </Form.Item>

                    <Form.Item
                        name="email"
                        rules={[
                            { required: true, message: '请输入邮箱！' },
                            { type: 'email', message: '请输入有效的邮箱地址！' }
                        ]}
                    >
                        <Input
                            prefix={<MailOutlined />}
                            placeholder="邮箱"
                        />
                    </Form.Item>

                    <Form.Item
                        name="password"
                        rules={[
                            { required: true, message: '请输入密码！' },
                            { min: 6, message: '密码至少6个字符！' }
                        ]}
                    >
                        <Input.Password
                            prefix={<LockOutlined />}
                            placeholder="密码"
                        />
                    </Form.Item>

                    <Form.Item
                        name="confirmPassword"
                        rules={[
                            { required: true, message: '请确认密码！' },
                            { min: 6, message: '密码至少6个字符！' }
                        ]}
                    >
                        <Input.Password
                            prefix={<LockOutlined />}
                            placeholder="确认密码"
                        />
                    </Form.Item>

                    <Form.Item>
                        <Button type="primary" htmlType="submit" block loading={loading}>
                            注册
                        </Button>
                    </Form.Item>
                </Form>
            </Card>
        </div>
    );
};

export default Register; 